package com.gitee.xmhzzz.component.spring.cloud.component.dao.interceptor;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.update.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName AbstractSqlCheckJsqlParserSupport
 * @Description
 * @Author wzq
 * @Date 2023/9/24 21:44
 * @Version 1.0
 */
public abstract class AbstractSqlCheckJsqlParserSupport {
    private static final Logger log = LoggerFactory.getLogger(AbstractSqlCheckJsqlParserSupport.class);

    public AbstractSqlCheckJsqlParserSupport() {
    }

    public String parserSingle(String sql, Long checkLimit) {
        try {
            Statement statement = CCJSqlParserUtil.parse(sql);
            return this.processParser(statement, 0, sql, checkLimit);
        } catch (JSQLParserException var4) {
            log.error("Failed to process, Error SQL: %s", var4.getCause(), sql);
            return sql;
        }
    }

    protected String processParser(Statement statement, int index, String sql, Long checkLimit) {
        if (!(statement instanceof Insert)) {
            if (statement instanceof Select) {
                this.processSelect((Select)statement, index, sql, checkLimit);
            } else if (statement instanceof Update) {
                this.processUpdate((Update)statement, index, sql);
            } else if (statement instanceof Delete) {
                this.processDelete((Delete)statement, index, sql);
            }
        }

        sql = statement.toString();
        log.info("SQL to parse, SQL: " + sql);
        return sql;
    }

    protected void processInsert(Insert insert, int index, String sql) {
        throw new UnsupportedOperationException();
    }

    protected void processDelete(Delete delete, int index, String sql) {
        throw new UnsupportedOperationException();
    }

    protected void processUpdate(Update update, int index, String sql) {
        throw new UnsupportedOperationException();
    }

    protected void processSelect(Select select, int index, String sql, Long checkLimit) {
        throw new UnsupportedOperationException();
    }
}
